import axios from 'axios'

// import { getItem, getToken } from "@/utils/token";
// import 'encryptlong';
// 创建axios副本
let baseUrl
console.log(process.env.VUE_APP_MODE, '6---');
if(process.env.VUE_APP_MODE === 'development'){
    baseUrl = '/api'
    //开发环境下的执行操作
}else if(process.env.VUE_APP_MODE === 'production'){
    baseUrl = 'http://bookkeeping.omisheep.cn/api'
    //生产环境下的执行操作
}
const request = axios.create({
    baseURL: baseUrl,
    // baseURL: '/api',
    // withCredentials: true
})

// 添加请求拦截器
request.interceptors.request.use(
    function (config) {
        if(config.url !== '/user/login') {
            const token = (JSON.parse(localStorage.getItem('token') as string)).accessToken
            if (token && config.headers) {
                config.headers.authorization = `Bearer ${token}`
            }
        }
        return config
    },
    function (error) {
        return Promise.reject(error)
    }
)

// 添加响应拦截器
request.interceptors.response.use(
    function (response) {
        if (response.status === 200) {
            return response.data
        } else {
            return Promise.reject(new Error(response.data.message))
        }
    },
    function (error) {
        return error.response.data;
    }
)

export default request

// 当发送的请求需要携带token时,设置请求头
// let token = null;
// if (config.app != null) {
//     token = getItem(config.app + '_token')
// } else {
//     token = getToken();
// }
// if (token !== null) {
//     config.headers.authorization = `Bearer ${token}`
// }
// if (config.rsa) {
//     let key = getToken('omisheep_key');
//     const enc = new JSEncrypt();
//     enc.setPublicKey(key);
//     let content = enc.encryptLong(JSON.stringify(config.data));
//     config.data = { content }
// }